<template>
  <el-card shadow="never">
    <div slot="header">
      <span class="machine-title">{{ $t('ToolingOff') }}</span>
    </div>
    <dynForm
      ref="dataForm"
      :info-form="searchForm"
      :select-list="selectList"
      :search-tags="searchTags">
      <template slot="prepend">
        <el-form-item
          :rules="[{ required: true, message: `${$t('pleaseInput')}${$t('toolingBarcode')}`, trigger: 'blur' }]"
          :label="$t('toolingBarcode')"
          prop="toolingBarcode"
        >
          <el-input
            v-model.trim="searchForm.toolingBarcode"
            :placeholder="`${$t('pleaseInput')}${$t('toolingBarcode')}`"
            clearable
            @keyup.enter.native="getEnterInfos"/>
        </el-form-item>
        <el-form-item
          :rules="[{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('eqpName')}`, trigger: 'change' }]"
          :label="$t('eqpName')"
          prop="eqpName"
        >
          <el-select
            v-model="searchForm.eqpName"
            :placeholder="`${$t('pleaseSelect')}${$t('eqpName')}`"
            disabled
            clearable>
            <el-option
              v-for="item in eqpNameList"
              :key="item.id"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </el-form-item>
      </template>
    </dynForm>
    <el-row>
      <div class="btn-row">
        <el-button icon="el-icon-circle-close" @click="handleClose">
          {{ $t("cancel") }}</el-button>
        <el-button
          type="primary"
          icon="el-icon-receiving"
          @click="handleSave"
        >{{ $t('save') }}</el-button>
      </div>
    </el-row>
  </el-card>
</template>

<script>
import { getFormInfos, doAdd } from '@/api/materialManagement/machineMaterial'
import { eqpIdNameListByMatCode } from '@/api/configSetting/materialMachineBinding'
export default {
  name: 'ToolingOff',
  data() {
    return {
      searchForm: {},
      selectList: {},
      eqpNameList: [],
      searchTags: Object.freeze(
        [
          { label: this.$t('toolingType'), prop: 'toolingType', tag: 'DynInput', disabled: true },
          { label: this.$t('toolingModel'), prop: 'toolingModel', tag: 'DynInput', disabled: true },
          { label: this.$t('currentTime'), prop: 'currentTime', tag: 'DynInput', disabled: true },
          { label: this.$t('lifeTime'), prop: 'lifeTime', tag: 'DynInput', disabled: true },
          { label: this.$t('remark'), prop: 'remark', tag: 'DynTextarea', row: 2, width: '100%', disabled: true }
        ]
      )
    }
  },
  created() {
    this.$nextTick(() => {
      this.$refs['dataForm'].$refs['dynForm'].clearValidate()
    })
  },
  methods: {
    async getEnterInfos() {
      this.$set(this.searchForm, 'eqpId', undefined)
      const res = await getFormInfos({ code: this.searchForm.code })
      this.getEqpIdNameListByMatCode({ code: this.searchForm.code })
      this.$betterTips(res, this.handleMerge.bind(this, res))
    },
    // 合并对象
    handleMerge(res) {
      this.searchForm = Object.assign({}, this.searchForm, res.data)
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    handleSave() {
      this.$refs['dataForm'].$refs['dynForm'].validate((valid) => {
        if (valid) {
          doAdd(this.$clearNull(this.searchForm)).then(res => {
            this.$betterTips(res, this.handleClose)
            this.$nextTick(() => {
              this.$refs['dataForm'].$refs['dynForm'].clearValidate()
            })
          })
        }
      })
    },
    async getEqpIdNameListByMatCode(materialCode) {
      const res = await eqpIdNameListByMatCode(materialCode)
      this.eqpIdList = res.data
    }
  }
}
</script>

<style scoped lang="scss">
.el-card {
  width: 1100px;
  margin: 20px auto;
}
::v-deep .el-form {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  .el-form-item {
    width: 48%;
    .el-form-item__content{
      width: 60%;
      .el-select {
        width: 100%;
      }
      .el-date-editor {
        width: 100%;
      }
    }
  }
}
.machine-title{
  font-size: 18px;
  font-weight: 500;
}
.btn-row{
  width: 90%;
  margin: 0 auto;
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
}
</style>
